package concurrencyPractice.chapter04;

import concurrencyPractice.ThreadSafe;

import java.util.List;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 程序4-18 通过组合实现“若没有则添加”
 * @date 2023/12/29 15:23:44
 */
public class ImprovedList<T> implements List<T> {

    private final List<T> list;
    public ImprovedList(List<T> list) {
        this.list = list;
    }

    @ThreadSafe
    public boolean putIfAbsent(T x) {
            boolean contains = !list.contains(x);

            if(contains) {
                list.add(x);
            }
            return contains;
    }
}
